package com.leetcode.problems121;


/**
 * https://leetcode-cn.com/problems/best-time-to-buy-and-sell-stock/
 * @Date 2021/11/20 14:34
 * @Author ghery
 * @Version V1.0
 */
public class Solution2 {


    public int maxProfit(int[] prices) {
        if(prices.length <= 1){
            return 0;
        }
        int buyPrice = prices[0];
        int cash = 0;
        for (int i = 1; i < prices.length; i++) {
           cash = Math.max(prices[i] - buyPrice,cash);
           buyPrice = Math.min(prices[i],buyPrice);
        }
        return cash;
    }
    public static void main(String[] args) {
        int[] prices = {7,6,8,3,9};
        System.out.println(new Solution2().maxProfit(prices));
    }
}
